/**
 * <copyright>
 * </copyright>
 *
 * $Id$
 */
package eistar.impl;

import java.util.List;

import ms.unicorn.dsl.eistar.impl.CompositeEvolutionBranch;
import ms.unicorn.dsl.eistar.impl.UnionModel;

import org.eclipse.emf.ecore.EClass;

import org.eclipse.emf.ecore.InternalEObject;
import org.eclipse.emf.ecore.impl.ENotificationImpl;
import eistar.EistarPackage;
import eistar.EvolutionBranch;
import eistar.EvolutionModel;
import eistar.Model;
import org.eclipse.emf.common.notify.Notification;
import eistar.EvolutionRule;

/**
 * <!-- begin-user-doc -->
 * An implementation of the model object '<em><b>Evolution Model</b></em>'.
 * <!-- end-user-doc -->
 * <p>
 * The following features are implemented:
 * <ul>
 *   <li>{@link eistar.impl.EvolutionModelImpl#getModel <em>Model</em>}</li>
 * </ul>
 * </p>
 *
 * @generated
 */
public class EvolutionModelImpl extends EntityImpl implements EvolutionModel {
	/**
	 * The cached value of the '{@link #getModel() <em>Model</em>}' reference.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @see #getModel()
	 * @generated
	 * @ordered
	 */
	protected Model model;

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	protected EvolutionModelImpl() {
		super();
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	protected EClass eStaticClass() {
		return EistarPackage.Literals.EVOLUTION_MODEL;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public Model getModel() {
		if (model != null && model.eIsProxy()) {
			InternalEObject oldModel = (InternalEObject)model;
			model = (Model)eResolveProxy(oldModel);
			if (model != oldModel) {
				if (eNotificationRequired())
					eNotify(new ENotificationImpl(this, Notification.RESOLVE, EistarPackage.EVOLUTION_MODEL__MODEL, oldModel, model));
			}
		}
		return model;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public Model basicGetModel() {
		return model;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public void setModel(Model newModel) {
		Model oldModel = model;
		model = newModel;
		if (eNotificationRequired())
			eNotify(new ENotificationImpl(this, Notification.SET, EistarPackage.EVOLUTION_MODEL__MODEL, oldModel, model));
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public Object eGet(int featureID, boolean resolve, boolean coreType) {
		switch (featureID) {
			case EistarPackage.EVOLUTION_MODEL__MODEL:
				if (resolve) return getModel();
				return basicGetModel();
		}
		return super.eGet(featureID, resolve, coreType);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public void eSet(int featureID, Object newValue) {
		switch (featureID) {
			case EistarPackage.EVOLUTION_MODEL__MODEL:
				setModel((Model)newValue);
				return;
		}
		super.eSet(featureID, newValue);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public void eUnset(int featureID) {
		switch (featureID) {
			case EistarPackage.EVOLUTION_MODEL__MODEL:
				setModel((Model)null);
				return;
		}
		super.eUnset(featureID);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public boolean eIsSet(int featureID) {
		switch (featureID) {
			case EistarPackage.EVOLUTION_MODEL__MODEL:
				return model != null;
		}
		return super.eIsSet(featureID);
	}

	public EvolutionRule combineRule(EvolutionRule r1, EvolutionRule r2) {
		if (r1 == null || r2 == null)
			return r1 != null ? r1 : r2;
				
		EvolutionRule result = new EvolutionRuleImpl();
		result.setOriginal(UnionModel.combineModel(r1.getOriginal(), r2.getOriginal()));
		
		for(EvolutionBranch b1: r1.getAllBranches())
			for (EvolutionBranch b2: r2.getAllBranches()) {
				EvolutionBranch b = CompositeEvolutionBranch.combineBranch(b1, b2);
				if (b.eContainer() != null)
					result.getRefs().add(b);
				else
					result.getChildren().add(b);
			}
		
		return result;
	}
	
	public EvolutionRule combineAllRules() {
		EvolutionRule result = null;
		for(EvolutionRule r: getRules())
			result = combineRule(result, r);
		return result;
	}
	
	@Override
	/**
	 * @generated NOT
	 */
	public List<EvolutionRule> getRules() {
		return getMembers(EvolutionRule.class);
	}
} //EvolutionModelImpl
